package models;
import java.util.Random;
/**
* A croupier class which controls gameplay
* 
* @author Kamil Olejnik
*/
public class Croupier { 
	   
       /**
       * Checking which player is next
       * @param actualPlayer Actual checked player
       * @param players[] an array of players in game
       * @param allinAllowed Assign acces to allIn
       * @return next next player
       *
       */
        public  static int whichPlayerNext2(int actualPlayer, Player players[], boolean allinAllowed){
                int next = whichPlayerNext(actualPlayer, players.length-1);
                if(allinAllowed){
                	
                			while(players[next].getStatus()==Player.PLAYER_OUT_OF_GAME)
                			 System.out.println("alowed");
                				next = whichPlayerNext(next, players.length-1);
                			
                	
                }
                else{
                	
                			while(players[next].getStatus()!=Player.PLAYER_IN_GAME)
                			{ System.out.println("else");
                                next = whichPlayerNext(next, players.length-1);
                			}
           
                		
                }
                return next;
        }

        public static int whichPlayerNext(int actualPlayer, int playersAmount ){
                int next = actualPlayer;
        next = (next+1)%playersAmount;
        return next;
    }
         
    public static int drawDealer(int playersAmount){
        Random random = new Random();
        return (random.nextInt(playersAmount)+1);
    }
    /*
        * Method determines which buttons can be pressed based on players coins, bet, maxBet and round
        * H-Check,B-Bet,R-Raise,C-Call,F-Fold,A-Allin,E-Exchange
        */
    public static String possibleMoves(int bet, int coins, boolean isBet, int maxBet,int round){
        String result = null;
        if(round==1){
                if(bet<maxBet){
                        if(coins==(maxBet-bet)) result = "CF";
                        else if(coins>(maxBet-bet))    result = "CRF";
                        else if(coins<(maxBet-bet))    result = "FA";
                }
                else if(bet==maxBet){
                        if(coins>0) result = "RF";
                        else   result = "FA";
                }
        }
        else{
                if (!isBet){
                        if (coins>0) result ="HBF";
                        else  result = "HF";//czy powinno byc allin?
                }
                else{
                        if(bet<maxBet){
                        if(coins==(maxBet-bet)) result = "CF";
                        else if(coins>(maxBet-bet))    result = "CRF";
                        else if(coins<(maxBet-bet))    result = "FA";
                    }
                    else if(bet==maxBet){
                        if(coins>0) result = "RF";
                        else   result = "FA";
                    }
                }
        }
        return result;
        }
        /**
        * Checkin if players habe the same bets
        * @param players[] an array of players in game
        * @return true if players have the same bets
        */
    public  static boolean havePlayersTheSameBets(Player players[]){
        int i = 0;
        while(players[i].getStatus()!=Player.PLAYER_IN_GAME){
                i++;
            }
            if (i==players.length){
                return true;
            }
            else{
                int bet = players[i].getBet();
                for(int j=0; j<players.length; j++){
                	if(players[j]!=null)
                        if( players[j].getStatus()==Player.PLAYER_IN_GAME && players[j].getBet()!=bet){
                                return false;
                        }
                }
                return true;
        }
    }
    /**
    * @param players[] an array of players in game
    * @return true if players have the same bets
    */
    public  static boolean havePlayersTheSameBets(int playersStatuses[],int playersBets[]){
        int i = 1;
        while(playersStatuses[i]!=Player.PLAYER_IN_GAME){
                i++;
            }
            if (i==playersStatuses.length){
                return true;
            }
            else{
                int bet = playersBets[i];
                for(int j=1; j<playersStatuses.length; j++){
                        if( playersStatuses[j]==Player.PLAYER_IN_GAME && playersBets[j]!=bet){
                                return false;
                        }
                }
                return true;
        }
    }
}